/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-12-22
 * Time: 下午3:38
 * To change this template use File | Settings | File Templates.
 */
Ext.define('Airways.controller.customer.CustController',{
    extend:'Ext.app.Controller',
    models:['customer.CustModel'],
    views:['customer.CustView'],
    stores:['customer.CustStore'],
    refs:[
        {ref:'custView',selector:'custView'},
        {ref:'custPopWindow',selector:'custPopWindow'},
        {ref:'custPopPanel',selector:'custPopPanel'}
    ],
    init:function(){
        var controller = this;
        this.control({
            'custView':{
                afterrender:function(){
                }
            },
            'custView button[action=find]':{
                click:function(){
                    this.getCustView().getStore().loadPage(1);
                }
            },
            'custView':{
                afterrender:function(){
                    var store = controller.getCustView().getStore();
                    var custName = controller.getCustView().down('textfield[name=custName]');
                    var custPhone = controller.getCustView().down('textfield[name=custPhone]');
                    var custType = controller.getCustView().down('textfield[name=custType]');
                    store.on({
                        beforeload:function(){
                            controller.record = null;
                            store.proxy.extraParams = {
                                custName:custName.getValue(),
                                custPhone:custPhone.getValue(),
                                custType:custType.getValue()
                            }
                        }
                    })
                },
                selectionchange:function(model,records){
                    if(records.length>0){
                        controller.record = records[0];
                    }
                }
            },
            'custView button[action=add]':{
                click:function(){
                    this.opType = "add";
                    var custPopWin = this.getCustPopWindow();
                    if(custPopWin == undefined){
                        custPopWin = Ext.create('Airways.view.customer.CustPopWindow');
                    }
                    custPopWin.setTitle("添加客户");
                    custPopWin.setIconCls("add");
                    custPopWin.show();
                }
            },
            'custView button[action=update]':{
                click:function(){
                    var record = controller.record;
                    if(record != null && record != ""){
                        this.opType = "edit";
                        var custPopWin = this.getCustPopWindow();
                        if(custPopWin == undefined){
                            custPopWin = Ext.create('Airways.view.customer.CustPopWindow');
                        }
                        custPopWin.setTitle("编辑客户");
                        custPopWin.setIconCls("update");
                        controller.getCustPopPanel().getForm().loadRecord(record);
                        custPopWin.show();
                    }else{
                        Ext.MessageBox.show({title:'提示',msg:'请先选中一行数据!',icon:Ext.MessageBox.WARNING,buttons:Ext.MessageBox.OK});
                        return ;
                    }
                }
            },
            'custView button[action=delete]':{
                click:function(){
                    var record = controller.record;
                    if(record != null && record != ""){
                        Ext.MessageBox.confirm("提示","确定要删除当前记录吗",function (e){
                            if( e == "yes"){
                                var url = basePath+'customer/custdel';
                                var params = new Object();
                                params.custId =  record.data.custId;
                                params.opType = "delete";
                                params.version = record.data.version;
                                callAjaxLocal(url,params,function(result){
                                    if(result.success){
                                        Ext.MessageBox.show({title:'提示',msg:'操作已成功!',icon:Ext.MessageBox.INFO,buttons:Ext.MessageBox.OK});
                                        controller.getCustView().getStore().loadPage(1);
                                    }else{
                                        Ext.MessageBox.show({title:'提示',msg:'操作失败!',icon:Ext.MessageBox.ERROR,buttons:Ext.MessageBox.OK});
                                    }
                                });
                            }
                        });
                    }else{
                        Ext.MessageBox.show({title:'提示',msg:'请先选中一行数据!',icon:Ext.MessageBox.WARNING,buttons:Ext.MessageBox.OK});
                        return ;
                    }
                }
            },
            'custPopWindow button[action=save]':{
                click:function(){
                    var form = controller.getCustPopPanel().getForm();
                    if(form.isValid()){
                        var url = basePath+'customer/custSave';
                        var params = form.getFieldValues();
                        params.opType = controller.opType;
                        callAjaxLocal(url,params,function(result){
                            if(result.success){
                                Ext.MessageBox.show({title:'提示',msg:'操作已成功!',icon:Ext.MessageBox.INFO,buttons:Ext.MessageBox.OK});
                                clearForm();
                                controller.getCustView().getStore().loadPage(1);
                            }else{
                                Ext.MessageBox.show({title:'提示',msg:'操作失败!',icon:Ext.MessageBox.ERROR,buttons:Ext.MessageBox.OK});
                            }
                        });
                    }else{
                        //验证失败
                        Ext.MessageBox.show({title:'提示',msg:'请按照提示填写数据!',icon:Ext.MessageBox.WARNING,buttons:Ext.MessageBox.OK});
                        return ;
                    }
                }
            },
            'custPopWindow button[action=cancel]':{
                click:function(){
                    clearForm();
                }
            },
            'custPopWindow':{
                close:function(){
                    clearForm();
                }
            }
        });
        function clearForm(){
            var form = controller.getCustPopPanel().getForm();
            controller.getCustPopWindow().hide();
            form.reset();
        }
    }
})
